package com.rzl.expense.agent;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据代理服务
 * @ClassName DataAgentService
 * @Author TMY
 * @Date 2022/8/11 22:23
 */
@Service
public class DataAgentService {

    private static final Logger log = LoggerFactory.getLogger(DataAgentService.class);

    @Value("${dataAgent.url}")
    private String agentUrl;

    @Value("${dataAgent.eleurl}")
    private String agentEleUrl;

    @Value("${dataAgent.renturl}")
    private String agentRentUrl;

    @Value("${dataAgent.twrurl}")
    private String agentTwrUrl;

    /**
     * 获取数据
     *
     * @param sql   sql
     * @param clazz clazz
     * @return {@link List}<{@link ?}>
     */
    @SuppressWarnings("unchecked")
	public List<?> getData(String sql, Class<?> clazz){
        String sqlInfo = "";
        try{
        	System.err.println("sqlInfo：" + sql);
            sqlInfo = DesUtil.encrypt(sql);
        }catch (Exception e){
           log.info("sql加密报错:{}",e);
        }
        Map<String,String> param = new HashMap<>();
        param.put("sqlInfo",sqlInfo);
        //调用doPost请求
        String jsonStr = HttpTookit.doPost(agentUrl,param);
        Result result = new Result();
        List<?> list = new ArrayList<>();
        List<Map<String,Object>> result_obj=null;
        if (StringUtils.isNoneBlank(jsonStr))
            result = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), Result.class);
        if("1".equals(result.getSuccess())){
            log.info("数据接口报错：{}",result.getMsg());
            return null;
        }else{
            if(null != result.getObj()){
                result_obj =  (List<Map<String,Object>>)result.getObj();
            }
            if(CollectionUtils.isNotEmpty(result_obj)){
                list = JSONObject.parseArray(JSONObject.toJSONString(result_obj), clazz);
            }
        }
        return list;

    }

    /**
     * @Description: 电费
     * @author tian
     * @date 2022年9月30日 上午10:29:24
     * @param clazz
     * @param param
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<?> getEleData(Class<?> clazz, Map<String, String> param){
        String jsonStr = HttpTookit.doPost(agentEleUrl, param);
        Result result = new Result();
        List<?> list = new ArrayList<>();
        List<Map<String,Object>> result_obj=null;
        if (StringUtils.isNoneBlank(jsonStr)) {
            result = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), Result.class);
        }
        if("1".equals(result.getSuccess())){
            log.info("数据接口报错：{}",result.getMsg());
            return null;
        }else{
            if(null != result.getObj()){
                result_obj =  (List<Map<String,Object>>)result.getObj();
            }
            if(CollectionUtils.isNotEmpty(result_obj)){
                list = JSONObject.parseArray(JSONObject.toJSONString(result_obj), clazz);
            }
        }
        return list;
    }

    /**
     * @Description: 租费
     * @author tian
     * @date 2022年9月30日 上午10:29:39
     * @param clazz
     * @param param
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<?> getRentData(Class<?> clazz, Map<String, String> param){
        String jsonStr = HttpTookit.doPost(agentRentUrl, param);
        Result result = new Result();
        List<?> list = new ArrayList<>();
        List<Map<String,Object>> result_obj=null;
        if (StringUtils.isNoneBlank(jsonStr)) {
            result = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), Result.class);
        }
        if("1".equals(result.getSuccess())){
            log.info("数据接口报错：{}",result.getMsg());
            return null;
        }else{
            if(null != result.getObj()){
                result_obj =  (List<Map<String,Object>>)result.getObj();
            }
            if(CollectionUtils.isNotEmpty(result_obj)){
                list = JSONObject.parseArray(JSONObject.toJSONString(result_obj), clazz);
            }
        }
        return list;
    }



    /**
     * @Description: 铁塔
     * @author tian
     * @date 2022年9月30日 上午10:29:53
     * @param clazz
     * @param param
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<?> getTwrData(Class<?> clazz, Map<String, String> param){
        String jsonStr = HttpTookit.doPost(agentTwrUrl, param);
        Result result = new Result();
        List<?> list = new ArrayList<>();
        List<Map<String,Object>> result_obj=null;
        if (StringUtils.isNoneBlank(jsonStr)) {
            result = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), Result.class);
        }
        if("1".equals(result.getSuccess())){
            log.info("数据接口报错：{}",result.getMsg());
            return null;
        }else{
            if(null != result.getObj()){
                result_obj =  (List<Map<String,Object>>)result.getObj();
            }
            if(CollectionUtils.isNotEmpty(result_obj)){
                list = JSONObject.parseArray(JSONObject.toJSONString(result_obj), clazz);
            }
        }
        return list;
    }
}
